package com.levtech.moviemanager.loan;

import com.levtech.moviemanager.MovieManagerApp;
import com.levtech.moviemanager.R;
import com.levtech.moviemanager.contact.Contact;
import com.levtech.moviemanager.db.DataStore;
import com.levtech.moviemanager.movie.Movie;
import com.levtech.moviemanager.types.Status;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class AddLoanActivity extends Activity
{
        
        private DataStore store;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_add_loan);
                this.store = ((MovieManagerApp)this.getApplication()).getStore();
                populateTitle();
                populateName();
                populateStatus();
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.add_loan, menu);
                return true;
        }
    	
    	@Override
    	public boolean onOptionsItemSelected(MenuItem selection)
    	{
    		switch(selection.getItemId())
    		{
    			case R.id.edit_loan:	//startActivity(new Intent(this, EditLoanActivity.class));
    									return true;
    									
    			case R.id.view_loan:	startActivity(new Intent(this, ViewLoanActivity.class));
										return true;
				
    			default: return super.onOptionsItemSelected(selection);
    		}
    	}
        
        public void addLoan(View view)
        {
                //Extract movie title from form
                Movie m = new Movie();
                m.setTitle(((Spinner)findViewById(R.id.movieField)).getSelectedItem().toString());
                
                //Extract contact first, last name from form
                String name = ((Spinner)findViewById(R.id.contactField)).getSelectedItem().toString();
                Contact c = new Contact(name.substring(name.indexOf(' ') + 1, name.length()), 
                                name.substring(0, name.indexOf(' ') - 1), "");
                
                //create loan object and execute db insert
                Loan l = new Loan(m, c, "02/14/2012", "02/14/2013",
                                ((Spinner)findViewById(R.id.statusField)).getSelectedItem().toString());
                this.store.insertLoan(l);
                
                //return to previous screen
                finish();
        }
    	
    	public void cancelAdd(View view)
    	{
    		finish();
    	}
        
        private void populateStatus()
        {
                Spinner status = (Spinner) findViewById(R.id.statusField);
                status.setAdapter(new ArrayAdapter<Status>(this, android.R.layout.simple_spinner_dropdown_item,
                                Status.values()));
        }
        
        private void populateTitle()
        {
                Spinner title = (Spinner) findViewById(R.id.movieField);
                title.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
                                 store.selectTitle()));
        }
        
        private void populateName()
        {
                Spinner title = (Spinner) findViewById(R.id.contactField);
                title.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
                                 store.selectName()));
        }
}